package com.scidpro;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.scidpro.util.Book;
import com.scidpro.util.LobLoader;
import com.scidpro.util.LobLoader.LoaderInterface;

public class AllBooksListActivity extends Activity {

	public static class BookListAdapter extends BaseAdapter {

		static class ViewHolder {
			TextView bookName;
			TextView bookPrice;
		}

		public final String TAG = this.getClass().getSimpleName();

		private LayoutInflater inflater;

		private ArrayList<Book> books;

		public BookListAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.book_list_item, null);

				holder = new ViewHolder();
				holder.bookName = (TextView) convertView.findViewById(R.id.book_title);
				holder.bookPrice = (TextView) convertView.findViewById(R.id.book_price);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			Book item = getItem(position);
			fillData(item, holder);

			return convertView;
		}

		private void fillData(Book book, ViewHolder holder) {
			holder.bookName.setText(book.name.trim());
			holder.bookPrice.setText(book.price.trim().toUpperCase());
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public int getCount() {
			if (books != null) {
				return books.size();
			}
			return 0;
		}

		@Override
		public Book getItem(int position) {
			if (books != null) {
				return books.get(position);
			}
			return null;
		}

		public void setData(ArrayList<Book> books) {
			this.books = books;
		}

	}

	protected static final String LOADER_KEY = "loader";

	private ListView list;
	private BookListAdapter adapter;

	private ProgressDialog progressDialog;
	private LobLoader loader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		list = new ListView(this);
		adapter = new BookListAdapter(this);

		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent i = new Intent(AllBooksListActivity.this, BookDescriptionActivity.class);
				i.putExtra("book", adapter.getItem(position));
				startActivity(i);
			}
		});

		setContentView(list);

		progressDialog = ProgressDialog.show(this, "Downloading list of books", getString(R.string.downloading), true, false);

		if (savedInstanceState == null) {
			loader = new LobLoader();
			loader.load(this);
		} else {
			loader = (LobLoader) savedInstanceState.getSerializable(LOADER_KEY);
			if (loader == null) {
				loader = new LobLoader();
				loader.load(this);
			}
		}

		loader.setLoaderInterface(new LoaderInterface() {
			@Override
			public void onPopulate(ArrayList<Book> data, boolean isLoading) {
				adapter.setData(data);
				list.invalidateViews();
				if (!isLoading && progressDialog != null) {
					progressDialog.dismiss();
				}
			}
		});
		loader.populate();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable(LOADER_KEY, loader);
	}

}
